import sequelize from './database.js';

async function seedDatabase() {
  try {
    console.log('Starting database seeding...');
    
    // 同步数据库结构
    console.log('Inserting users...');
    await sequelize.query(`
      INSERT INTO users (phone, password, nickname, avatar, status, createdAt, updatedAt)
      VALUES 
        ('15079962544', '$2a$10$6Bnv6HbKx.V4yX3z3FnqI.T3AeJHwoYxwR5wt3hPWg5OC9jKGq1Vy', '用户A', 'default-avatar.png', 'offline', NOW(), NOW()),
        ('17779865994', '$2a$10$6Bnv6HbKx.V4yX3z3FnqI.T3AeJHwoYxwR5wt3hPWg5OC9jKGq1Vy', '用户B', 'default-avatar.png', 'offline', NOW(), NOW())
    `);

    // 获取用户ID
    console.log('Getting user IDs...');
    const [users] = await sequelize.query('SELECT id, phone FROM users');
    console.log('Found users:', users);
    
    const user1 = users.find(u => u.phone === '15079962544');
    const user2 = users.find(u => u.phone === '17779865994');
    console.log('User1:', user1);
    console.log('User2:', user2);

    if (user1 && user2) {
      console.log('Inserting friendships...');
      await sequelize.query(`
        INSERT INTO friendships (userId, friendId, status, remark, createdAt, updatedAt)
        VALUES 
          (${user1.id}, ${user2.id}, 'accepted', '我的好友B', NOW(), NOW()),
          (${user2.id}, ${user1.id}, 'accepted', '我的好友A', NOW(), NOW())
      `);
    } else {
      console.log('Users not found!');
    }

    console.log('Database seeded successfully');
  } catch (error) {
    console.error('Error seeding database:', error);
    console.error('Error stack:', error.stack);
  } finally {
    await sequelize.close();
  }
}

// 如果这个文件被直接运行
if (process.argv[1] === new URL(import.meta.url).pathname) {
  seedDatabase().then(() => process.exit());
}

export default seedDatabase;
